package cms.service.access;

import cms.bean.ErrorView;
import cms.bean.user.AccessUser;
import cms.common.data.ApiResult;
import cms.common.data.ApiResultCode;
import cms.common.exception.ServiceException;
import cms.manage.SettingManage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 模块访问频率
 */
@Service
public class SubmitQuantityAccessService implements AccessService<String> {
    @Resource
    SettingManage settingManage;

    @Override
    public ApiResult access(AccessUser accessUser, String act, HttpServletRequest request, String module) {
        String fullModule = act + module;
        //统计每分钟原来提交次数
        Integer original = settingManage.getSubmitQuantity(fullModule, accessUser.getUserName());
        if (original != null) {
            settingManage.addSubmitQuantity(fullModule, accessUser.getUserName(), original + 1);//刷新每分钟原来提交次数
        } else {
            settingManage.addSubmitQuantity(fullModule, accessUser.getUserName(), 1);//刷新每分钟原来提交次数
        }
        //统计每分钟原来提交次数
        Integer quantity = settingManage.getSubmitQuantity(fullModule, accessUser.getUserName());
        if (quantity != null && quantity > 30) {//如果每分钟提交超过30次，则一分钟内不允许'取消隐藏'
            throw new ServiceException(ApiResultCode.NO_PERMISSION.setMessage(ErrorView._1640.name()));
//            error.put("topicUnhide", ErrorView._1640.name());//提交过于频繁，请稍后再提交
        }
        return ApiResult.buildSuccess(fullModule);
    }
}
